<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of SQLDatabase
 *
 * @author vlad
 */
class SQLDatabase extends SQLStruct {
    
    private $_tableNames;
    private $_tables;
    
    public function __construct($dbh, $dbName = "") {
        $this->setDBHandle($dbh);
        if (!empty($dbName)) {
            $this->_dbh->SelectDatabase($dbName);
        } else {
            $dbName = $this->_dbh->GetSelectedDatabase();
        }
        if (!empty($dbName)) {
            $this->_name = $dbName;
            $this->collectInformation($dbName);
        }
        
        return $this;
    }
    
    public function collectInformation() {
        print "Collecting info on ".$this->_name." database\n";
        $this->GetTableNames();
        foreach ($this->_tableNames as $table) {
            // print "SQLDatabase::conn -> ".$this->_dbh->isConnected();
            $this->_tables[] = new SQLTable(&$this->_dbh, $table);
        }
        // $this->_dbh->getSelectedDatabase();
        return $this;
    }
    
    public function GetTableNames() {
        $this->_tableNames = array();
        $res = $this->_dbh->Q("show tables;");
        while ($row = mysql_fetch_array($res)) {
            $this->_tableNames[] = $row[0];
        }
        return $this;
    }
    
    public function GetTables() {
        return $this->_tables;
    }
    
    public function __toString() {
        return $this->_name;
    }
    
    
}

?>
